package com.mazegame.frontend;

import com.mazegame.backend.BackgroundPanel;
import com.mazegame.backend.ImageDisplayPane;
import com.mazegame.backend.UserDAO;
import javafx.application.Platform;
import javafx.embed.swing.JFXPanel;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.VBox;
import javafx.scene.text.Font;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Region;
import javafx.scene.paint.Color;
import javafx.scene.text.FontWeight;

import javax.swing.*;
import java.awt.*;


public class ChapterPanel extends JPanel implements GamePanel{
    private CardLayout cardLayout;
    private JPanel cardPanel;
    private JLayeredPane layeredPane;
    private static final int WIDTH = 1500;
    private static final int HEIGHT = 900;
    private JFXPanel jfxPanel;
    // 获取当前登录的用户名和ID
    private static int userId;
    public static int currentChapter;
    private  static Button chapter1Button;
    private static Button chapter2Button;
    private static Button chapter3Button;

    public ChapterPanel(CardLayout cardLayout, JPanel cardPanel) {
        this.cardLayout = cardLayout;//
        this.cardPanel = cardPanel;
        initializeUI();
    }

    private void initializeUI() {
        // 使用 JLayeredPane 管理不同层级的组件
        layeredPane = new JLayeredPane();
        setLayout(new BorderLayout());
        add(layeredPane, BorderLayout.CENTER);

        // 创建背景面板并添加到最底层
        BackgroundPanel backgroundPanel = new BackgroundPanel("/img/章节界面.png");
        backgroundPanel.setBounds(0, 0, WIDTH, HEIGHT);
        layeredPane.add(backgroundPanel, JLayeredPane.DEFAULT_LAYER);

        // 创建 JavaFX 内容面板（登录界面）
        jfxPanel = new JFXPanel();
        jfxPanel.setOpaque(false); // 确保 JFXPanel 透明
        jfxPanel.setBounds(0, 0, WIDTH, HEIGHT);
        layeredPane.add(jfxPanel, JLayeredPane.MODAL_LAYER); // 放在上层

        Platform.runLater(() -> {
            // 创建主容器
            BorderPane root = createMainPane();
            //设置root透明
            root.setStyle("-fx-background-color: rgba(0, 0, 0, 0);");
            // 创建透明场景
            Scene scene = new Scene(root, WIDTH, HEIGHT, Color.TRANSPARENT);
            jfxPanel.setScene(scene);
        });
    }

    private BorderPane createMainPane() {
        BorderPane root = new BorderPane();
        root.setTop(createTitleRegion());
        root.setCenter(createContentRegion());
        return root;
    }
    private Region createContentRegion() {
        javafx.scene.control.Label tip1 = new javafx.scene.control.Label("30金币可解锁");
        javafx.scene.control.Label tip2 = new javafx.scene.control.Label("60金币可解锁");
        javafx.scene.control.Label tip3 = new javafx.scene.control.Label("90金币可解锁");
        tip1.setFont(Font.font("Arial", FontWeight.BOLD, 28));
        tip1.setStyle("-fx-text-fill: white;");
        tip2.setFont(Font.font("Arial", FontWeight.BOLD, 28));
        tip2.setStyle("-fx-text-fill: white;");
        tip3.setFont(Font.font("Arial", FontWeight.BOLD, 28));
        tip3.setStyle("-fx-text-fill: white;");
        HBox titleBox = new HBox(280);
        titleBox.getChildren().addAll(tip1, tip2, tip3);
        titleBox.setAlignment(Pos.CENTER);
        // 创建水平布局的按钮容器
        HBox contentBox = new HBox();
        contentBox.setPrefSize(WIDTH, 500);
        contentBox.setPadding(new Insets(200,0,0,0));
        contentBox.setSpacing(150); // 设置按钮之间的间距
        contentBox.setAlignment(Pos.TOP_CENTER); // 水平居中
        // 创建三个按钮，并设置样式和初始状态
        chapter1Button = createChapterButton("第一章");
        chapter2Button = createChapterButton("第二章");
        chapter3Button = createChapterButton("第三章");
        // 初始状态：只有第一章可点击
        chapter1Button.setDisable(true);
        chapter2Button.setDisable(true);
        chapter3Button.setDisable(true);
        // 设置按钮点击事件
        chapter1Button.setOnAction(e -> {
            cardLayout.show(cardPanel, "FIRSTSTORYPANEL");
            BufferPanel.num = 1;
            MazeGeneratePanel.num = 1;
        });
        chapter2Button.setOnAction(e -> {
            cardLayout.show(cardPanel, "SECONDSTORYPANEL");
            BufferPanel.num = 2;
            MazeGeneratePanel.num = 2;
            // 第二章逻辑
        });
        chapter3Button.setOnAction(e -> {
            cardLayout.show(cardPanel, "THIRDSTORYPANEL");
            BufferPanel.num = 3;
            MazeGeneratePanel.num = 3;
            // 第三章逻辑
        });
        // 将按钮添加到布局中
        contentBox.getChildren().addAll(chapter1Button, chapter2Button, chapter3Button);
        titleBox.setPadding(new Insets(320, 0, 0, 0));
        contentBox.setPadding(new Insets(-200, 0, 0, 0));
        VBox root = new VBox(20);
        root.getChildren().addAll(titleBox, contentBox);
        //root.setAlignment(Pos.TOP_CENTER);
        return root;
    }

    public static void update(){
        //根据userId匹配数据库中用户信息，获取chapter信息
        userId = Integer.parseInt(LoginPanel.currentUserId);
        currentChapter = UserDAO.getChapter(userId);
        System.out.println("currentUserId: " + userId);
        System.out.println("currentChapter: " + currentChapter);
        System.out.println("currentScore: " + UserDAO.getScore(userId));
        // 根据 currentChapter 设置可点击的按钮
        if (currentChapter >= 3 && UserDAO.getScore(userId) >= 90) {
            chapter1Button.setDisable(false);
            chapter2Button.setDisable(false);
            chapter3Button.setDisable(false);
            System.out.println("3");
        }
        else if (currentChapter >= 2 && UserDAO.getScore(userId) >= 60) {
            chapter1Button.setDisable(false);
            chapter2Button.setDisable(false);
            chapter3Button.setDisable(true);
            System.out.println("2");
        }
        else if (currentChapter >= 1 && UserDAO.getScore(userId) >= 30) {
            chapter1Button.setDisable(false);
            chapter2Button.setDisable(true);
            chapter3Button.setDisable(true);
            System.out.println("1");
        }
        else{
            chapter1Button.setDisable(true);
            chapter2Button.setDisable(true);
            chapter3Button.setDisable(true);
        }


    }

    // 创建一个圆角矩形按钮的方法
    private Button createChapterButton(String text) {
        Button button = new Button(text);
        button.setPrefSize(300, 200);
        button.setStyle(
                "-fx-background-color: rgba(173, 216, 230, 0.7); " +
                        "-fx-text-fill: white; " +
                        "-fx-font-size: 54px; " +
                        "-fx-background-radius: 50; " +
                        "-fx-border-radius: 50;"
        );

        // 添加悬停效果
        button.setOnMouseEntered(e -> button.setStyle(
                "-fx-background-color: rgba(173, 216, 230, 0.9); " + // 更不透明
                        "-fx-text-fill: white; " +
                        "-fx-font-size: 54px; " +
                        "-fx-background-radius: 50; " +
                        "-fx-border-radius: 50;"
        ));
        button.setOnMouseExited(e -> button.setStyle(
                "-fx-background-color: rgba(173, 216, 230, 0.7); " +
                        "-fx-text-fill: white; " +
                        "-fx-font-size: 54px; " +
                        "-fx-background-radius: 50; " +
                        "-fx-border-radius: 50;"
        ));

        return button;
    }

    private Region createTitleRegion() {
        HBox titleBox = new HBox();
        titleBox.setPadding(new Insets(20, 0, 0, 30));
        // 设置宽高
        titleBox.setPrefSize(WIDTH, 150);
        // 设置背景颜色,辅助布局
        //titleBox.setStyle("-fx-background-color: rgb(255,255,255);");
        javafx.scene.control.Button button = new Button();
        ImageDisplayPane imageDisplayPane = new ImageDisplayPane("/img/返回按钮.png", 80, 80, 0);
        button.setGraphic(imageDisplayPane);
        button.setStyle("-fx-background-color:transparent; -fx-background-radius: 200;");
        button.setEffect(null);
        button.setPrefSize(100, 100);
        // 设置最大宽高度
        button.setMaxSize(100, 100);
        // 添加悬停效果
        button.setOnMouseEntered(e -> {
            button.setStyle("-fx-background-color: rgba(255,255,255,0.5); -fx-background-radius: 200;");
        });
        button.setOnMouseExited(e -> {
            button.setStyle("-fx-background-color: transparent; -fx-background-radius: 200;");
            button.setEffect(null);
        });
        // 添加点击效果
        button.setOnAction(e -> {
            cardLayout.show(cardPanel, "OPTIONPANEL");
        });
        titleBox.getChildren().add(button);
        return titleBox;
    }

    private BorderPane createContentPane() {
        BorderPane root = new BorderPane();
        return root;
    }

    @Override
    public void showPanel() {
        cardLayout.show(cardPanel, "CHAPTERPANEL");
    }

    @Override
    public void cleanup() {

    }
}
